package com.example.smartcity_poverty.ui.AllService.LogisticsQuery.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import com.example.smartcity_poverty.App
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.LogisticsQueryBean.LqBannerModel
import com.example.smartcity_poverty.logic.ServiceBean.LogisticsQueryBean.LqShowModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.activity_logistics_query.*
import kotlinx.android.synthetic.main.activity_lq_show.*
import kotlinx.coroutines.Dispatchers

/**
 * 物流公司详情
 */
class LqShowActivity : AppCompatActivity() {

    private var id: Int? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_lq_show)
        title = "物流公司详情"
        id = intent.getStringExtra("id")?.toInt()


        runThread()
    }

    private fun runThread() = coroutine(Dispatchers.Main) {
        // banner
        setBanner()

        // 公司信息
        setShowList()
    }

    /**
     * 公司信息
     */
    private suspend fun setShowList() {
        api.getLqShowData(getToken(), id!!).apply {
            if (this.code == 200) {
                this.data?.let {
                    runOnUiThread {
                        lqShowListTv1.text = it.introduce
                        lqShowListTv2.text = it.shippingMethod
                    }
                }
            } else runOnUiThread { this.msg?.show() }
        }
    }


    /**
     * TODO 轮播图
     */
    private var arrayBanner = ArrayList<LqBannerModel.DataBean>()
    private suspend fun setBanner() {
        if (getToken().isNotEmpty()) {
            api.getLqBanner(getToken()).apply {
                if (this.code == 200) {
                    arrayBanner.addAll(this.data!!)
                    runOnUiThread {
                        lqShowBanner.apply {
                            this.adapter =
                                object : BannerImageAdapter<LqBannerModel.DataBean>(arrayBanner) {
                                    override fun onBindView(
                                        p0: BannerImageHolder?,
                                        p1: LqBannerModel.DataBean?,
                                        p2: Int,
                                        p3: Int
                                    ) {
                                        glide(url + p1?.imgUrl, p0?.imageView!!)
                                    }
                                }
                            this.addBannerLifecycleObserver(this@LqShowActivity).indicator =
                                CircleIndicator(context)
                            this.setBannerRound(25f)
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        } else runOnUiThread { "请先登录！".show() }
    }


    /**
     * TODO　填充数据
     */


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}